package keeto.com.vn.bms.ui.activity;

import java.util.ArrayList;

import keeto.com.vn.bms.config.BMSCore;
import keeto.com.vn.bms.model.BaseInfor.ProductInfor;
import keeto.com.vn.bms.network.IBMSProtocol;
import keeto.com.vn.bms.ui.adapter.ListProductAdapter;
import keeto.com.vn.bms.ui.widget.LogoutDialog;
import keeto.com.vn.bms.utils.BMSListener;
import keeto.com.vn.bms.utils.BMSSharedPreferences;
import keeto.com.vn.bms.utils.Utils;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

public class ListProductActivity extends FragmentActivity implements
OnClickListener, OnRefreshListener<ListView>, OnItemClickListener {

	private ArrayList<ProductInfor> mArrayList = new ArrayList<ProductInfor>();

	private ListProductAdapter mAdapter;

	private PullToRefreshListView lvProduct;
	private ListView mListView;
	private ImageButton btnBack;
	private TextView tvTitleName;
	private ImageButton btnLogon;
	private BMSSharedPreferences mPreferences;
	private ProgressBar pgbar;
	private int pageNumber=1;

	private int pageLimit=10;
	private Listenner listenner = new Listenner();


	public static void actionLaunch(Context context) {
		Intent intent = new Intent(context, ListProductActivity.class);
		context.startActivity(intent);
	}

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.fragment_list_infor);
		mPreferences = new BMSSharedPreferences(this);
		initViewComponent();

	}

	private void initViewComponent() {
		btnBack = (ImageButton) findViewById(R.id.btn_tittle_back);
		tvTitleName = (TextView) findViewById(R.id.tv_title_name);
		btnLogon = (ImageButton) findViewById(R.id.btn_tittle_login);
		lvProduct = (PullToRefreshListView) findViewById(R.id.lst_product);
		tvTitleName.setText(getString(R.string.txt_grid_home_item));
		mListView = lvProduct.getRefreshableView();
		pgbar=(ProgressBar)findViewById(R.id.pgbar);
		lvProduct.setMode(Mode.PULL_FROM_START);
		btnLogon.setBackgroundResource(Utils.getLogobyPermission(mPreferences.getUserPermission(this)));

		btnBack.setOnClickListener(this);
		btnLogon.setOnClickListener(this);
		lvProduct.setOnRefreshListener(this);
		lvProduct.setOnItemClickListener(this);

		mAdapter = new ListProductAdapter(ListProductActivity.this,mArrayList );
		mListView.setAdapter(mAdapter);
		BMSCore.getInstance(this).taskGetListProduct(pageLimit, pageNumber, listenner);
		pgbar.setVisibility(View.VISIBLE);
	}



	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		DetailProductInfoActivity.actionLaunchViewInfo(ListProductActivity.this, mArrayList.get(position-1));
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_tittle_back:
			this.finish();
			break;
		case R.id.btn_tittle_login:
			if (mPreferences.getUserAuthKey(ListProductActivity.this).equals("")){
				LoginBMSActivity.actionLaunch(ListProductActivity.this);
			}else{
			LogoutDialog dialog = new LogoutDialog(ListProductActivity.this);
			dialog.show();
			}
			break;

		default:
			break;
		}
	}

	@Override
	public void onRefresh(PullToRefreshBase<ListView> refreshView) {
		if(pageNumber>0){
			BMSCore.getInstance(ListProductActivity.this).taskGetListProduct(pageLimit, pageNumber, listenner);
		}else{
			new Handler().post(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					lvProduct.onRefreshComplete();
				}
			});

		}

	}


	class Listenner extends BMSListener {

		@Override
		public void finishGetListProducts(final int returnCode, final int pageCount,
				final ArrayList<ProductInfor> arrayList) {
			// TODO Auto-generated method stub
			super.finishGetListProducts(returnCode, pageCount, arrayList);
			runOnUiThread(new Runnable() {
				public void run() {
					pgbar.setVisibility(View.GONE);
					if(returnCode==IBMSProtocol.STATUS_CODE_OK){
						if (pageNumber < pageCount){
							pageNumber +=1;
						}else{
							pageNumber=-1;
						}
						for(ProductInfor infor: arrayList){
							mArrayList.add(infor);
						}
						mAdapter.notifyDataSetChanged();
					}else if(returnCode==IBMSProtocol.STATUS_CODE_EXIT_RECORD){
						Toast.makeText(ListProductActivity.this, ListProductActivity.this.getResources().getString(R.string.error_data_server), Toast.LENGTH_SHORT).show();
					}else if(returnCode==IBMSProtocol.STATUS_CODE_GENERIC_FAIL_CONECTTION){
						Toast.makeText(ListProductActivity.this, ListProductActivity.this.getResources().getString(R.string.error_network_connection), Toast.LENGTH_SHORT).show();
					}else if(returnCode==IBMSProtocol.STATUS_CODE_GENERIC_FAIL){
						Toast.makeText(ListProductActivity.this, ListProductActivity.this.getResources().getString(R.string.error_perpose), Toast.LENGTH_SHORT).show();
					}
					lvProduct.onRefreshComplete();
				}
			});
		}
	}

}
